/*	
 * 	v0.2新增功能
 *	添加打开和关闭动画功能 
 * 
 * 	返回模态框对象obj，关闭时，使用obj.close可手动关闭
 * 
 * 	v0.3新增功能
 * 	新增模态框打开前回调showBefore()，
 * 	打开后回调showAfter()，
 * 	关闭前回调hideBefore()，
 * 	关闭后回调hideAfter()
 * 
 * 	v0.4新增功能
 * 		默认打开为淡入效果
 * 		默认关闭为淡出效果
 * 		
 * 		可以根据设置type的类型，显示等待加载框，iframe框，modal框，alert框，confirm框，prompt,button框以及居中的tips
 * 		当type=prompt时，proMsg和proVal参数有效
 * 		当type等于iframe时，在顶层窗口弹出iframe框，
 * 			其他都在当前窗口操作的（导致窗口直在本页居中，无法在浏览器中居中），
 * 
 * 	v0.5 新增功能
 * 		解决调用页面含有父窗口时，能在浏览器中居中的问题
 * 
 * 	v0.6 新增功能
 * 		toolTip功能，能在 左、右、上、下显示，超出显示不了换边显示
 * 
 * 	v0.7 新增功能
 * 		prompt功能  可以设置为select、checkbox、radio、text、password、file、number、email、textarea等表单类型
 * 							返回类型为JSON数组
 * 		新增isFooter属性，可设置是否显示按钮
 * 		新增关闭按钮的回调函数
 * 
 * 		$.MaxWating()  	加载等带框
 * 		$.MaxAlert()  	alert框
 * 		$.MaxConfirm()  confirm框
 * 		$.MaxPrompt()  	prompt框
 * 		$.MaxProver() 	tip居中框
 * 		$.MaxIframe()  	Iframe框
 * 		$.MaxModal()  	模态框 
 * 		
 * 		$.MaxDialog.defaults   暴露插件默认参数
 * 		$.MaxDialogFormat()		 设置全局默认参数
 * 
 * 	v0.8 功能
 * 		优化
 * 			当animateTime未设置时，关闭时，立即执行hideAfter回调。打开时，立即执行showAfter回调
 * 			打开多个弹出框，后面打开的弹出框的zIndex依次增加
 * 		新增
 * 			type == iframe时，且esc为true,打开的第一个iframe的具有esc退出功能，同时存在2个及以上的iframe框时，esc退出功能无效。
 * 
 *	Ahthor：MaxJh
 * 	Copyright：MaxJh
 * 	Version：maxModal-0.8.js
 * */

;(function(win,$){
	win.MaxDialog = function(config){
		this.curWin = win.self;
		this.parWin = win.parent || this.$getParentWin();
		this.topWin = win.top;
		this.config = {};
		//默认参数扩展
		if(config/* && $.isPlainObject(config)*/){
			$.extend(true,this.config,MaxDialog.defaults,config);
		}
		this.config.timer = new Date().getTime();
		this.config.id = "maxModal_" + (this.config.id ? this.config.id : this.config.timer);
		//添加css样式文件
		this.addCssStyle();
		
		//创建基本的DOM
		this.body = $("body");
		if(this.config.type == "iframe"){
			this.body = $("body",this.topWin.document);
		}else{
			this.body = $("body");
		}
		//创建遮罩层
		this.mask = $('<div class="maxModalmask" id="'+ this.config.id +'"></div>');
		//创建遮罩层
		this.cover = $('<div class="maxModalcover"></div>');
		//创建模态框
		this.win = $('<div class="maxModal"></div>');
		//创建模态框头部
		this.winHeader = $('<div class="maxModal_header"></div>');
		//创建标题栏的关闭按钮
		this.winClosBtn = $('<div class="iconClose"></div>')
		//创建模态框内容
		this.winContent = $('<div class="maxModal_body"></div>');
		//创建模态框底部
		this.winFooter = $('<div class="maxModal_footer"></div>');
		
		//窗口改变模态框居中
		this.creat();
		this.resize(this.mask,this.win,this.config);
		
		if(this.config.type == "iframe"){
			this.topWin.MaxDialog._DiagArray.push(this);
			this.onEvent();
		}
		return this;
	};
	
	//设置模态框的层级
	MaxDialog.zIndex = 999999999;
	
	// 记录打开的窗口数
	MaxDialog._DiagArray = [];
	
	MaxDialog.defaults = {
		"id":null,										//模态框的id
		"type":null,									//默认为等待加载框，可以为iframe,modal,alert，confirm，prompt,tips,button（只居中）,
		"esc": false,									// 值在type=iframe时有效，且打开的一个iframe。
		"promptData":{									//只在type=prompt时，该值有效
			"proMsg":"prompt标题",						//只在type=prompt时，pormpt的标题	
			"inputType":"text",							//表单类型，可以为select、textarea、file、checkbox、radio、password、number等，默认为text
			"inputOptions":[							//表单的默认参数
				{
					"text": 'prompt文本值',				//表单的文本值
        			"value": 'prompt Value值'			//表单的value值
				}
			]
		},
		"data":null,									// 传递到iframe的值
		"isFooter":true,								//是否显示按钮组，默认为true
		"url":null,										//只在type=iframe时，该值有效，设置iframe的src的值
		"width":null,									//模态框的宽度
		"height":null,      							//模态框的高度
		"title":"标题",									//模态框的标题
		"content":null,									//模态框的内容				
		"time":0,										//模态框自动关闭时间，影响动画关闭的时间
		"opacity":0,									//遮罩层的透明度，默认0.3
		"maskClose":false,								//点击遮罩层是否关闭，默认不关闭
		"animateTime":null,								//动画作用时间设定，默认为1s
		"animateInClass":"fadeIn",						//打开时的动画类样式，可自定义（动画时间为1s），建议使用animate.css。当值为空，无动画效果，animateTime无效
		"animateOutClass":"fadeOut",					//关闭时的动画类样式，可自定义（动画时间为1s），建议使用animate.css。当值为空，无动画效果，animateTime无效	
		"footerAlign":"right",							//按钮组的显示位置，默认为right，可设置“left”，“center”
		//按钮组
		"buttons":[										//按钮组会被外部buttons参数全部替换（浅拷贝），
														//如果需要保持默认的按钮组参数写入外部buttons参数中
			{
			"text":"确定",								//按钮的文本内容
				"type":"Msuccess",						//按钮的类样式，可自定义,默认有Msuccess,Mcancel,Mclose三种样式
				"events":"click",						//按钮的默认事件触发类型，默认为click
				"callback":null							//按钮触发事件的回调函数,如果想点击按钮后不关闭模态框，
														//在回调函数中添加 return false 则不会关闭模态框
			},
			{
				"text":"取消",
				"type":"Mcancel",
				"events":"click",
				"callback":null
			}/*,
			{
				"text":"关闭",
				"type":"Mclose",
				"events":"click",
				"callback":null
			}*/
		],
		"showBefore":null, 								//模态框打开前执行的回调函数
		"showAfter":null,								//模态框打开后执行的回调函数
		"hideBefore":null, 								//模态框关闭前执行的回调函数
		"hideAfter":null,								//模态框关闭后执行的回调函数
		"close":null									//点击关闭按钮时的回调函数
	};
	MaxDialog.prototype = {
		$getParentWin: function() {
			var parentWin = win;
		    while (parentWin != parentWin.parent) {
		        if (parentWin.parent.document.getElementsByTagName("FRAMESET").length > 0) break;
		        parentWin = parentWin.parent;
		    }
		    return parentWin;
		},
		$getTopWin: function() {
			var parentWin = win;
			// window.top!=window.self
		    while (parentWin != parentWin.self) {
		        if (parentWin.parent.document.getElementsByTagName("FRAMESET").length > 0) break;
		        parentWin = parentWin.parent;
		    }
		    return parentWin;
		},
		//添加模态框的css样式
		addCssStyle:function(){
			var headDom = "";
			if(this.config.type == "iframe"){
				headDom = $(window.top.document).find("head");
			}else{
				headDom = $(document).find("head");
			}
			if(headDom.find("style.maxModalCss").length == 0){//如果没有模态框样式，则添加
				var styles ="<style type='text/css' class='maxModalCss'>"+
							".maxModalmask{position:fixed;left:0;top:0;width:100%;height:100%;}" +
							".maxModalmask .maxModalcover{position:fixed;top:0;left:0;width:100%;height:100%;background-color:#111;opacity:0.3;filter:Alpha(Opacity:30);}"+
							".maxModalmask .maxModal{position:absolute;max-width:100%;max-height:100%;min-width: 320px;background-color:#fff;overflow:hidden;}"+
							".maxModalmask .maxModal .maxModal_header{position:relative;padding:6px;background:#09c;color:#fff;box-sizing: border-box;}"+
							".maxModalmask .maxModal .maxModal_header .iconClose{position:absolute;top:3px;right:3px;width:24px;height:24px;background:url() no-repeat;cursor:pointer;}"+
							".maxModalmask .maxModal .maxModal_body{padding:6px;overflow:auto;min-height:40px;box-sizing: border-box;}"+
							".maxModalmask .maxModal .maxModal_body_border{border: 3px solid #0097cb;padding:0}"+
							".maxModalmask .maxModal .maxModal_body>iframe{width:100%;height:100%;border:none!important;}"+
							".maxModalmask .maxModal_body .maxModal_prompt {width: 100%;padding:6px;}"+
							".maxModalmask .maxModal_body .maxModal_prompt>.maxModal_prompt_tit {width:100%;padding-bottom:6px;vertical-align: top;}"+
  							".maxModalmask .maxModal_body .maxModal_prompt>input,"+
  							".maxModalmask .maxModal_body .maxModal_prompt>select,"+
  							".maxModalmask .maxModal_body .maxModal_prompt>textarea {text-indent: 6px;width:100%}"+
  							".maxModalmask .maxModal_body .maxModal_prompt>label {padding-left:12px;display:block;}"+
  							".maxModalmask .maxModal_body .maxModal_prompt>label>input {vertical-align: middle;}"+
							".maxModalmask .maxModal .maxModal_footer{border-top:1px solid #ddd;padding:6px;text-align:right;box-sizing: border-box;}"+
							".maxModalmask .maxModal_footer .maxModal_button{display:inline-block;padding:6px 12px;margin:0 3px;color:#ebebeb;font-size:12px;text-align:center;vertical-align:middle;cursor:pointer;}"+
							/*".maxModalmask .maxModal_footer .maxModal_button:hover{opacity:.8;filter:alpha(opacity:80);cursor:pointer;}"+*/
							".maxModalmask .maxModal_footer .maxModal_button.Msuccess{background-color:#327ab7;}"+
							".maxModalmask .maxModal_footer .maxModal_button.Msuccess:hover{background-color:#286090;}"+
							".maxModalmask .maxModal_footer .maxModal_button.Mcancel{background-color:#d9534f;}"+
							".maxModalmask .maxModal_footer .maxModal_button.Mcancel:hover{background-color:#c9302c;}"+
							".maxModalmask .maxModal_footer .maxModal_button.Mclose{background-color:#E41010;}"+
							".maxModalmask .maxModal_footer .maxModal_button.Mclose:hover{background-color:#E41010;}"+
							".animated{-webkit-animation-duration:1s;animation-duration:1s;-webkit-animation-fill-mode:both;animation-fill-mode:both;}"+
							"@-webkit-keyframes fadeIn{from{opacity:0;}to{opacity:1;}}"+
							"@keyframes fadeIn{from{opacity:0;}to{opacity:1;}}"+
							".fadeIn{-webkit-animation-name:fadeIn;animation-name:fadeIn;}"+
							"@-webkit-keyframes fadeOut{from{opacity:1;}to{opacity:0;}}"+
							"@keyframes fadeOut{from{opacity:1;}to{opacity:0;}}"+
							".fadeOut{-webkit-animation-name:fadeOut;animation-name:fadeOut;}"+
							"</style>";
				headDom.append(styles);
			}
		},
		//创建dom
		creat:function(){
			var _this_ = this,
				config = this.config,
				mask = this.mask,
				cover = this.cover,
				win = this.win,
				header = this.winHeader,
				closeBtn = this.winClosBtn,
				content = this.winContent,
				footer = this.winFooter,
				body = this.body,
				type = config.type;
			
			//模态框打开前的回调函数
			if(config.showBefore){
				config.showBefore.call(this,content,this);
			}
			//标题
			this.creatHead();
			//内容
			this.creatContent();
				
			//按钮区
			if(config.isFooter){
				if( !(type == null || type == "tips") ){
					this.creatButtons(footer,config.buttons);
					win.append(footer);
				}
			}
			
			//将遮罩层插入到模态框
			mask.append(cover);
			//模态框整体插入到模态框
			mask.append(win);
			//给模态框整天添加动画函数的作用时间
			if (config.animateTime) {
				mask.css({
					"animation-duration":config.animateTime +"s"
				});
				win.css({
					"animation-duration":config.animateTime +"s"
				});
			}
			//添加启动动画，animated 是amimate.css的类样式
			if(config.animateInClass){
				mask.addClass("animated "+config.animateInClass);
				win.addClass("animated "+config.animateInClass);
			}
			
			//模态框遮罩层插入到body
			body.append(mask);	
				
			//根据配置参数初始化样式
			this.initStyle();
			
			//模态框居中
			this.center(mask,win,config);
			//模态框打开后的回调函数
			if(config.showAfter){
				var t = config.animateTime?config.animateTime: 0;
				setTimeout(function(){
					config.showAfter.call(this,content,this);
				},t * 1000)
				/*config.showAfter();*/
			}
			//绑定方法
			this.initMethod();
		},
		onEvent: function(){
			var _this_ = this;
			var topWin = _this_.topWin;
			var curWin = _this_.curWin;
			var topDoc = topWin.document;
			var curDoc = curWin.document;
			var arr = topWin.MaxDialog._DiagArray;
			var len = arr.length;
			// 当弹出第一个iframe框时，
			// 若在顶层窗口打开iframe框，则topWin === curWin；
			// _this_.topWin.MaxDialog._DiagArray.length === 1 && topWin === curWin,取消click,keyup事件，给topWindow.document绑定keyup事件。
		
			// 否则curWin !== curWin
			// _this_.topWin.MaxDialog._DiagArray.length === 1 && topWin !== curWin,
			// 1、默认焦点在curWin页，取消click,keyup事件，给curWin.document绑定keyup事件。
			// 2、点击topWin页，取消click,keyup事件，给topWin.document绑定keyup事件。
			
			// 当弹出第二个iframe框时，topWin !== curWin
			// _this_.topWin.MaxDialog._DiagArray.length > 1
			// 禁用esc退出功能，且将arr中的前面的diag取消绑定的事件
			
			// 1、默认焦点在curWin页，给curWin.document绑定keyup事件。并监听topWin.document._DiagArray的长度，若长度有变化，则取消curWin.document的keyup事件。
			// 2、点击topWin页，取消curWin.document的事件绑定。给topWin.document绑定keyup事件。并监听topWin.document._DiagArray的长度，若长度小于1，则取消topWin.document的keyup事件
			
			if(len === 1 && topWin === curWin){
				_this_.offEvents();
				
				$(topDoc).on('keyup.myPlugin', function(e){
					var ev = e || topWin.event;
					ev.stopPropagation();
					var keyCode = ev.which;
					if(keyCode == 27){
						_this_.offEvents();
						arr[len - 1].config.esc && arr[len - 1].close();
					}
				});
			}else if(len === 1 && topWin !== curWin){
				_this_.offEvents();
				// 当前窗口绑定keyup事件
				$(curDoc).on('keyup.myPlugin', function(e){
					var ev = e || curWin.event;
					ev.stopPropagation();
					var keyCode = ev.which;
					if(keyCode == 27){
						_this_.offEvents();
						arr[len - 1].config.esc && arr[len - 1].close();
					}
				});
				
				$(topDoc).on('click.myPlugin', function(e){
					_this_.offEvents();
					var ev = e || topWin.event;
					ev.stopPropagation();
					$(topDoc).on('keyup.myPlugin', function(e){
						var ev = e || topWin.event;
						ev.stopPropagation();
						var keyCode = ev.which;
						if(keyCode == 27){
							_this_.offEvents();
							arr[len - 1].config.esc && arr[len - 1].close();
						}
					});
				});
				
			}else if(len > 1){
				arr[len-2].offEvents();
				_this_.offEvents();
				
				/*// 当前窗口绑定keyup事件
				$(curDoc).on('keyup.myPlugin', function(e){
					console.log('curWin ...')
					_this_.offEvents();
					var ev = e || curWin.event;
					ev.stopPropagation();
					var keyCode = ev.which;
					if(keyCode == 27){
						arr[len - 1].close();
						arr[len - 2].onEvent();
					}
				});
				
				$(_this_.topWin.document).on('click.myPlugin', function(){
					console.log('click ...')
					_this_.offEvents();
					$(_this_.topWin.document).on('keyup.myPlugin', function(){
						_this_.offEvents();
						arr[len - 1].close();
					});
				});*/
				
				
			}

		},
		offEvents: function(){
			$(this.topWin.document).off('click.myPlugin');
			$(this.topWin.document).off('keyup.myPlugin');
			$(this.curWin.document).off('keyup.myPlugin');
		},
		set: function(key, value){
			this[key] = value;
		},
		get: function(key) {
			return this[key];
		},
		//创建加载等待
		creatWating:function(){
			var config = this.config,
				win = this.win;
			//如果没有传递任何配置参数
				win.css({
					"width":"auto",
					"height":"auto",
					"background-color":"transparent"
				});
				//等待图片
				win.html("<img src=''>");
		},
		//创建iframe框
		creatIframe:function(){
			var config = this.config,
				win = this.win,
				content = this.winContent;
			this.data = config.data;
			//内容区
			if(config.url){
				content.html("<iframe src='"+config.url+"' frameBorder='0'></iframe>" || "请设定iframe的URL地址！");
				win.append(content);
			}else{
				content.html(config.content || "请设定URL地址，content的含有iframe的html！");
				win.append(content);
			}
		},
		//创建模态框
		creatModal:function(){
			var config = this.config,
				win = this.win,
				content = this.winContent;
				//内容区
				if(config.content){
					content.html(config.content || "请设定你需要显示的内容或HTML");
					win.append(content);
				}else{
					content.html("请设定你需要显示的内容或HTML");
					win.append(content);
				}
		},
		//创建alert弹出框
		creatAlert:function(){
			var config = this.config,
				win = this.win,
				content = this.winContent;
				//内容区
				if(config.content){
					content.html(config.content);
					win.append(content);
				}else{
					content.append("警告！！！");
					win.append(content);
				}
		},
		//创建confirm弹出框
		creatConfirm:function(){
			var config = this.config,
				win = this.win,
				content = this.winContent;
				//内容区
				if(config.content){
					content.html(config.content);
					win.append(content);
				}else{
					content.append("Confirm框！！！");
					win.append(content);
				}
		},
		//创建prompt弹出框
		creatPrompt:function(){
			var config = this.config,
				win = this.win,
				content = this.winContent,
				promptData = config.promptData;
				var optionsData = "";
				if(promptData.inputType == "select"){
					optionsData = "<select class='maxModal_inputType' name='maxModal_inputType'>";
					for (var i = 0,len=promptData.inputOptions.length; i < len; i++) {
						optionsData+= "<option value='"+promptData.inputOptions[i].value+"'>"+promptData.inputOptions[i].text+"</option>"
					}
					optionsData += "</select>";
				}else if(promptData.inputType == "textarea"){//文本域
					optionsData = "<textarea class='maxModal_inputType' name='maxModal_inputType' value='"+promptData.inputOptions[0].value+"'>"+promptData.inputOptions[0].text+"</textarea>";
				}else if(promptData.inputType == "checkbox" || promptData.inputType == "radio"){//多选或单选
					optionsData = "";
					for (var i = 0,len=promptData.inputOptions.length; i < len; i++) {
						optionsData+= "<label class='maxModal_inputType'><input type='"+promptData.inputType+"' name='maxModal_inputType' value='"+promptData.inputOptions[i].value+"'/>"+promptData.inputOptions[i].text+"</label>" ;
					}
					optionsData += "";
				}else{//input框，类型有text,checkbox,radio,email,number,password,file
					optionsData = "<input class='maxModal_inputType' name='maxModal_inputType' type='"+promptData.inputType+"' value='"+promptData.inputOptions[0].value+"'>";
				}
				var	inputHTml = "<div class='maxModal_prompt'>";
				if(promptData.proMsg){
					inputHTml += "<div class='maxModal_prompt_tit'>"+promptData.proMsg+"：</div>";
				}
				inputHTml += 	optionsData+"</div>";
				
				//内容区
				content.append(inputHTml)
				win.append(content);
		},
		//创建Tips弹出框
		creatTips:function(){
			var config = this.config,
				header = this.winHeader,
				win = this.win;
				header.find("div.iconClose").show();
				content = this.winContent;
				//内容区
				content.html(config.content || "tips提示内容");
				win.append(content);
		},
		//关闭模态框
		close:function(){
			var config = this.config;
			var win = this.win;
			var mask = this.mask;
			var content = this.winContent;
			
			//关闭前回调函数
			if(config.hideBefore){
				config.hideBefore.call(this,content,this);
			}
			
			//自动延时关闭操作
			if(config.animateTime){
				//关闭动画
				if(config.animateOutClass){
					//添加关闭动画类样式
					mask.removeClass(config.animateInClass).addClass(config.animateOutClass);
					win.removeClass(config.animateInClass).addClass(config.animateOutClass);
				}
				//动画时间默认为1s
				win.css({
					"animation-duration":+ config.animateTime - 0.05 +"s"
				});
				//延时关闭
				setTimeout(function(){
					mask.remove();
					//关闭后回调函数
					if(config.hideAfter){
						config.hideAfter.call(this,content,this);
					}
				},config.animateTime * 1000);
			}else{
				// mask.hide();
				mask.remove();
				//关闭后回调函数
				if(config.hideAfter){
					config.hideAfter.call(this,content,this);
				}
			}
			if(config.type == 'iframe'){
				this.offEvents();
				this.topWin.MaxDialog._DiagArray.pop();
			}
		},
		//方法
		initMethod:function(){
			var _this_ = this;
			var config = this.config,
				closeBtn = this.winClosBtn,
				win = this.win;
				mask = this.mask;
			//根据配置参数设定点击遮罩层是否关闭模态框
			if(config.maskClose){
				//遮罩层绑定点击事件
				mask.on("click",function(){
					_this_.close();
				})
			};
			if(config.time){
				setTimeout(function(){
					_this_.close();
				},config.time * 1000);
			};
			//标题栏关闭按钮绑定事件
			closeBtn.on("click",function(e){
				var ev = e || window.event;
				ev.stopPropagation();
				ev.preventDefault();
				_this_.close();
				if(config.close){
					config.close();
				}
			})
			
			//阻止模态框事件冒泡
			win.on("click",function(e){
				var ev = e || window.event;
				ev.stopPropagation();
			//	ev.preventDefault();
			});
		},
		//初始化模态框的config的样式
		initStyle:function(){
			var _this_ = this,
				config = this.config,
				mask = this.mask,
				cover = this.cover,
				win = this.win,
				header = this.winHeader,
				content = this.winContent,
				footer = this.winFooter,
				body = this.body;
			var type = config.type;
			//设置遮罩层透明度
			if(config.opacity){
				cover.css({
					"filter":"Alpha(opacity:"+ config.opacity * 100 +")",
					"opacity":config.opacity
				})
			}
			//设置遮罩层层级
			MaxDialog.zIndex ++;
			this.topWin.MaxDialog.zIndex = this.topWin.MaxDialog.zIndex * 1 + 1;
			mask.css({
				"z-index": this.topWin.MaxDialog.zIndex ? this.topWin.MaxDialog.zIndex : MaxDialog.zIndex
			})
		
			if(type == "modal" || type == "iframe"){
				if(!config.isFooter){
					content.addClass("maxModal_body_border");
				}
				//设置模态框的宽度
				if(config.width){
					win.css({
						"width":config.width
					});
				}else{
					win.css({
						"max-width":"80%"
					})
				}
				//设置模态框的高度
				if(config.height){
					win.css({
						"height":config.height
					})
				}else{
					win.css({
						"max-height":"80%"
					})
				};
			}else if(type == null){
				win.css({
						"width":"auto",
						"min-width":"auto"
				});
			}else{
				win.css({
						"width":config.width || "320"
				});
				//设置模态框的高度
				if(config.height){
					win.css({
						"height":config.height
					})
				}else{
					win.css({
						"max-height":"80%"
					})
				}
			}
			
			//设置按钮组显示的位置
			if(config.buttons){
				if(config.footerAlign != "right"){
					footer.css({
						"text-align":config.footerAlign										
					});
				}
			}
		},
		//模态框居中
		center:function(mask,win,config){
			var mask_W = $(mask).width();
			var mask_H = $(mask).height();
			var win_W = $(win).width();
			var win_H = $(win).height();
			var t = ( mask_W - win_W ) / 2 ;
			var l = ( mask_H - win_H ) / 2 ;
			$(win).css({
				"left":t,
				"top":l
			})
				//设置模态框body部分的高度
				var Modalheight = 0,
					ModalHeader_h = 0,
					ModalBody_H = 0,
					ModalFooter_h = 0;
				//获取标题栏的高度
				if(config.title){
					ModalHeader_h = this.winHeader.outerHeight();
				}
				//获取按钮组的高度
				if(config.buttons){
					ModalFooter_h = this.winFooter.outerHeight();
				}
				//设置模态框body的高度
				if(config.height){
					//设置模态框整体的高度
					Modalheight = win.outerHeight();
					//获取模态框body的高度
					ModalBody_H = Modalheight - ModalHeader_h - ModalFooter_h ;
					this.winContent.css({
						"height":ModalBody_H
					})
				}else{
					Modalheight = mask_H * 0.8;
					//获取模态框body的高度
					ModalBody_H = Modalheight - ModalHeader_h - ModalFooter_h ;
					this.winContent.css({
						"max-height":ModalBody_H
					})
				}
		},
		resize:function(){
			var _this_ = this;
			$(window).resize(function(){
				_this_.center(_this_.mask,_this_.win,_this_.config);
			})
		},
		//创建标题
		creatHead:function(){
			var type = this.config.type;
			if(type != null || (type == "tips"/* && this.config.side == "center"*/)){
				var config = this.config,
					win = this.win,
					header = this.winHeader,
					closeBtn = this.winClosBtn;
				//标题
				header.append(config.title);
				header.append(closeBtn)
				win.append(header);
				//如果存在按钮组，则隐藏右上角的关闭按钮
				if(config.isFooter){
					header.find("div.iconClose").hide();
				}else{
					header.find("div.iconClose").show();
				}
			}
		},
		//创建内容区
		creatContent:function(){
			var type = this.config.type;
			if(type == null){
				this.creatWating();
			}else if(type == "modal"){
				this.creatModal();
			}else if(type == "iframe"){
				this.creatIframe();
			}else if(type == "alert" || type == "button"){
				this.creatAlert();
			}else if(type == "confirm"){
				this.creatConfirm();
			}else if(type == "prompt"){
				this.creatPrompt();
			}else if(type == "tips"){
				this.creatTips();
			}
		},
		//创建按钮组
		creatButtons:function(footer,buttons){
			var _this_ = this;
            var content = _this_.winContent;
			var fType = this.config.type;
			$(buttons).each(function(i){
				if(fType == "alert" && i == 1){//alert框按钮组
					return false;
				}else if((fType == "confirm" || fType == "prompt") && i == 2){//confirm,prompt按钮组
					return false;
				}else if(fType == "tips" && i == 0){//tips框按钮组
					return false;
				}else if(fType == null){
					return false;
				}
				
				//获取按钮的类样式
				var type = this.type?" "+this.type:" Msuccess";
				var text = this.text?this.text:"按钮"+(++i);
				var callback = this.callback?this.callback:null;
				var button = $("<a class='maxModal_button"+type+"'>"+text+"</a>");
				if(fType == "prompt" && !(this.text)){
					return;
				}
				//按钮绑定事件
				if(callback){
					var events = this.events?this.events:"click";
					button.on(events,function(event){
						var ev = event || window.event;
						ev.stopPropagation();
						ev.preventDefault();
						var arg = '';
						if(fType == "prompt" && i==0){
							//返回值
							var inputType = _this_.config.promptData.inputType;
							var dataArr = [];
							var dataJson = {};
							
							if(inputType == "select"){
								dataJson.value = $(".maxModal_inputType").val();
								dataJson.text  = $(".maxModal_inputType option:selected").text();
								dataArr.push(dataJson);
							}/*else if(inputType == "textarea"){
								dataJson.value = $(".maxModal_inputType").val();
								dataJson.text  = "";
								dataArr.push(dataJson);
							}*/else if(inputType == "checkbox" || inputType == "radio"){
								$("input[name='maxModal_inputType']:checked").each(function(){
									dataJson = {};
									dataJson.value = $(this).val();
									dataJson.text  = $(this).parents("label").text();
									dataArr.push(dataJson);
								});
							}else{
								dataJson.value = $(".maxModal_inputType").val();
								dataJson.text  = "";
								dataArr.push(dataJson);
							}
							arg = dataArr;
						}
						// 如果类型为prompt则传递值，如果为iframe则返回iframe对应的jquery对象，iframe可以使用iframe.find('iframe').contents()[0] 获取iframe页面内的document对象
						var isClose = callback.call(this,arg, content, this);
						if(isClose != false){
							_this_.close();
						}
					})
				}else{
					button.on("click",function(e){
						_this_.close();
					})
				}
				footer.append(button);
			});
		},
		//
	};
	$.MaxinitZindex = function(){}
	//返回模态框对象
	$.MaxDialog = function(config){
		$.MaxinitZindex();
		return new MaxDialog(config);
	};
	//暴露MaxDialog的默认参数
	$.MaxDialog.defaults = MaxDialog.defaults;
	
	//获取maxDialog的索引
	$.getMaxDialogIndex = function(){
		return MaxDialog.zIndex;
	}
	//设置maxDialog的索引
	$.setMaxDialogIndex = function(index){
		MaxDialog.zIndex = index;
	}
	
	//设置全局默认参数
	$.MaxDialogFormat = function(config){
		$.extend(true, MaxDialog.defaults, config);
	}
	
	//alert弹出窗
	$.MaxWating = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":""});
		return new MaxDialog(MaxTemp);
	};
	
	//alert弹出窗
	$.MaxAlert = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"alert"});
		return new MaxDialog(MaxTemp);
	};
	
	//comfirm弹出窗
	$.MaxConfirm = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"comfirm"});
		return new MaxDialog(MaxTemp);
	};
	
	//prompt弹出窗
	$.MaxPrompt = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"prompt"});
		return new MaxDialog(MaxTemp);
	};
	
	//iframe弹出窗
	$.MaxIframe = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"iframe"});
		return new MaxDialog(MaxTemp);
	};
	
	//模态框
	$.MaxModal = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"modal"});
		return new MaxDialog(MaxTemp);
	};
	
	//居中弹出
	$.MaxProver = function(config){
		var MaxTemp = {};
		$.extend(true,MaxTemp,config,{"type":"tips"});
		return new MaxDialog(MaxTemp);
	};
	
	//绑定到window上的插件调用方法
	win.maxWating = function(){
		//var MaxTemp = {};
		//$.extend(true,MaxTemp,{},{});
		return new MaxDialog();
	}
	
	win.MaxAlert = function(msg,cb){
		var MaxTemp = {};
		$.extend(true,MaxTemp,{"content":msg,"type":"alert","buttons":[{"callback":cb}]});
		return new MaxDialog(MaxTemp);
	};
	
	win.MaxConfirm = function(msg,cb){
		var MaxTemp = {};
		$.extend(true,MaxTemp,{"content":msg,"type":"confirm","buttons":[{"callback":cb}]});
		return new MaxDialog(MaxTemp);
	};
	
	win.MaxProver = function(msg,cb){
		var MaxTemp = {};
		$.extend(true,MaxTemp,{"content":msg,"type":"tips","buttons":[{"callback":cb}]});
		return new MaxDialog(MaxTemp);
	};
	
	//
	win.dialog = {
		alert:function(msg,cb){
			win.MaxAlert(msg,cb);
		},
		confirm:function(msg,cb){
			win.MaxConfirm(msg,cb);
		},
		prover:function(msg,cb){
			win.MaxProver(msg,cb);
		}
	}
	
})(window,jQuery);
